package com.woniu.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.project.core.dto.ResultDto;
import com.woniu.project.core.exception.ProjectProductException;
import com.woniu.project.dto.productbrand.ProductBrandSelectDto;
import com.woniu.project.params.productbrand.ProductBrandAddParam;
import com.woniu.project.params.productbrand.ProductBrandSelectParam;
import com.woniu.project.params.productbrand.ProductBrandUpdateParam;
import com.woniu.project.service.IProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/productBrand")
@Api(tags="品牌管理")
public class ProductBrandController {
    @Autowired
    IProductBrandService productBrandService;

    //根据是否有name进行分页查询
    @GetMapping("/select")
    public ResultDto select(@ModelAttribute ProductBrandSelectParam productBrandSelectParam) {

        try {
            IPage<ProductBrandSelectDto> productBrandIpage = productBrandService.select(productBrandSelectParam);
            return ResultDto.success(200, "查询成功", productBrandIpage);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品品牌查询系统异常");
        }

    }
    @ApiOperation(value="新增商品品牌",notes = "id不填,其他参数均必填")
    //新增商品品牌
    @PostMapping("/addBrand")
    public ResultDto addBrand(@Valid @RequestBody ProductBrandAddParam productBrandAddParam) {
        try {
            int result = productBrandService.addBrand(productBrandAddParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "新增商品品牌成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品品牌新增系统异常");
        }
    }
    @ApiOperation(value="修改商品品牌",notes = "参数全必填")
    //修改商品品牌
    @PutMapping("/updateBrand")
    public ResultDto updateBrand(@Valid @RequestBody ProductBrandUpdateParam productBrandUpdateParam) {
        try {
            int result = productBrandService.updateBrand(productBrandUpdateParam);
            if (result == 1) {
                return ResultDto.success(500, "该序号已存在，请重新输入", null);
            }
            return ResultDto.success(200, "修改商品品牌成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品品牌修改系统异常");
        }
    }
    @ApiOperation(value="根据id删除品牌",notes = "id必填")
    //根据id删除品牌
    @DeleteMapping("/deleteById/{id}")
    public ResultDto deleteById(@PathVariable Integer id) {
        try {
            productBrandService.deleteById(id);
            return ResultDto.success(200, "删除商品品牌成功", null);
        } catch (ProjectProductException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new ProjectProductException(500, "商品品牌删除系统异常");
        }
    }
}

